import { DeployFunction } from 'hardhat-deploy/types'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { developmentChains, networkConfig } from '../helper-hardhat-config'

interface CCIPConfig {
  sourceRouter_?: string
  linkToken_?: string
  destinationRouter_?: string
}

const nFTPoolBurnAndMint: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
  const { firstAccount } = await hre.getNamedAccounts()
  const { deploy, log } = hre.deployments

  log('Deploying NFTPoolBurnAndMint...')

  let destChainRouter: string = ''
  let linkTokenAddr: string = ''
  // 如果是本地测试，则从CCIPLocalSimulator合约中获取NFTPoolBurnAndMint合约构造函数所需要的参数
  if (developmentChains.includes(hre.network.name)) {
    const ccipSimulatorDeployment = await hre.deployments.get('CCIPLocalSimulator')
    // 获取CCIPLocalSimulator合约部署后的实例
    const ccipSimulator = await hre.ethers.getContractAt('CCIPLocalSimulator', ccipSimulatorDeployment.address)
    // 从CCIPLocalSimulator合约中获取NFTPoolBurnAndMint合约构造函数所需要的参数
    const ccipConfig = (await ccipSimulator.configuration()) as unknown as CCIPConfig
    destChainRouter = ccipConfig.destinationRouter_!
    linkTokenAddr = ccipConfig.linkToken_!
  } else {
    // 测试网测试
    destChainRouter = networkConfig[hre.network.config.chainId as keyof typeof networkConfig].router!
    linkTokenAddr = networkConfig[hre.network.config.chainId as keyof typeof networkConfig].linkToken!
  }

  const wnftDeployment = await hre.deployments.get('WrappedMyToken')
  const wnftAddr = wnftDeployment.address

  await deploy('NFTPoolBurnAndMint', {
    contract: 'NFTPoolBurnAndMint',
    from: firstAccount,
    log: true,
    args: [destChainRouter, linkTokenAddr, wnftAddr]
  })
  log('NFTPoolBurnAndMint contract deployed successfully!')
}

nFTPoolBurnAndMint.tags = ['destchain', 'all']

export default nFTPoolBurnAndMint
